package com.aiflow.Action;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.aiflow.db.ConnDB;

public class HeraldAction {

	// 查询 告警信息
	public void findAlarms() {
		HttpServletRequest req = ServletActionContext.getRequest();
		int start = Integer.parseInt(req.getParameter("start"));
		int limit = Integer.parseInt(req.getParameter("limit"));
		String name = (String) req.getParameter("name");
		int end = start + limit;
		int begin = start + 1;
		ConnDB db = new ConnDB();
		ResultSet rs = null;
		JSONObject json = new JSONObject();
		List<Map<String, String>> datalist = new ArrayList<Map<String, String>>();
		JSONArray datas = new JSONArray();
		String whereSql = "";
		if(name != null && !name.equals("")){
			whereSql = " and  d.name like '%"+name+"%' ";
		}
		String sqlCount = "";
		Connection con = db.getConnection();
		PreparedStatement ps = null;

		try {
			String sql = "select * from "
					+ "("
					+ "select h.id,d.name as alarm_name,alarm as alarm_id,alarm_target,target_name,pattern,staTime,endTime,case when  pulse = '1' then '开启' when  pulse = '0' then '关闭' end as pulse,(row_number() over(ORDER BY h.id desc)) rb  from dbo.t_s_herald h,dictionary d where h.alarm = d.id "+whereSql+") tt "
					+ "where tt.rb between " + begin + " and " + end;

			sqlCount = "select h.id  from dbo.t_s_herald h,dictionary d where h.alarm = d.id ";
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();

			while (rs.next()) {
				Map<String, String> mapdata = new HashMap<String, String>();
				mapdata.put("id", rs.getString(1));
				mapdata.put("alarm_name", rs.getString(2));
				mapdata.put("alarm_id", rs.getString(3));
				mapdata.put("alarm_target", rs.getString(4));
				mapdata.put("target_name", rs.getString(5));
				mapdata.put("pattern", rs.getString(6));
				mapdata.put("staTime", rs.getString(7));
				mapdata.put("endTime", rs.getString(8)); // ,pattern,staTime,endTime
				mapdata.put("pulse", rs.getString(9));

				datalist.add(mapdata);
			}
			int index = db.GetDataCount(sqlCount);
			datas = JSONArray.fromObject(datalist);
			json.put("success", true);
			json.put("datas", datas);
			json.put("results", index);
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			out.print(json);
			out.close();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				ps.close();
				con.close();
			} catch (Exception ex1) {
				ex1.printStackTrace();
			}
		}
	}

	// 新增传报
	public void addHerald() {
		ConnDB db = new ConnDB();
		PreparedStatement ps = null;
		String alarm_id = ServletActionContext.getRequest().getParameter(
				"alarm_id");
		String targetName = ServletActionContext.getRequest().getParameter(
				"targetName");
		String alarmTargetId = ServletActionContext.getRequest().getParameter(
				"checkid");
		String startTime = ServletActionContext.getRequest().getParameter(
				"startTime");
		String endTime = ServletActionContext.getRequest().getParameter(
				"endTime");

		String pattern = ServletActionContext.getRequest().getParameter(
				"pattern");
		
		String pulse = ServletActionContext.getRequest().getParameter(
		"pulse");

		Connection con = db.getConnection(); // checkid
		try {
			String sql = "insert into dbo.t_s_herald(alarm,alarm_target,target_name,staTime , endTime,pattern,pulse) values(?,?,?,?,?,?,?)";
			try {
				ps = con.prepareStatement(sql);
				int index = 0;
				ps.setString(++index, alarm_id);
				ps.setString(++index, alarmTargetId);
				ps.setString(++index, targetName);
				ps.setString(++index, startTime);
				ps.setString(++index, endTime);
				ps.setString(++index, pattern);
				ps.setString(++index, pulse);

				int rs = ps.executeUpdate();
				if (rs > 0) {
				//	System.out.println("保存成功!!!");
				}
				JSONObject json = new JSONObject();
				json.put("success", true);
				HttpServletResponse response = ServletActionContext
						.getResponse();
				response.setCharacterEncoding("UTF-8");
				PrintWriter out = response.getWriter();
				out.print(json);
				out.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				ps.close();
				con.close();
			} catch (Exception ex1) {
				ex1.printStackTrace();
			}
		}
	}

	// 查询 告警对象
	public void findAlarmsTarget() {
		HttpServletRequest req = ServletActionContext.getRequest();
		int start = Integer.parseInt(req.getParameter("start"));
		int limit = Integer.parseInt(req.getParameter("limit"));
		int end = start + limit;
		int begin = start + 1;
		ConnDB db = new ConnDB();
		ResultSet rs = null;
		JSONObject json = new JSONObject();
		List<Map<String, String>> datalist = new ArrayList<Map<String, String>>();
		JSONArray datas = new JSONArray();
		String whereSql = "";
		String sqlCount = "";
		Connection con = db.getConnection();
		PreparedStatement ps = null;

		try {
			String sql = "select * from " + "("
					+ "select t.id,t.alarmTarget_name, "
					+ "(row_number() over(ORDER BY id desc)) rb "
					+ "from t_s_alarmTarget where ) tt "
					+ "where tt.rb between " + begin + " and " + end;
			sqlCount = "select id,alarm_name "
					+ "from t_s_alarmTarget where 1=1 " + whereSql;
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();

			while (rs.next()) {
				Map<String, String> mapdata = new HashMap<String, String>();
				mapdata.put("id", rs.getString(1));
				mapdata.put("alarm_name", rs.getString(2));

				datalist.add(mapdata);
			}
			int index = db.GetInfoCount(sqlCount);
			datas = JSONArray.fromObject(datalist);
			json.put("success", true);
			json.put("datas", datas);
			json.put("results", index);
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			out.print(json);
			out.close();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				ps.close();
				con.close();
			} catch (Exception ex1) {
				ex1.printStackTrace();

			}
		}

	}

	// 修改传报
	public void updateHeral() {
		ConnDB db = new ConnDB();
		PreparedStatement ps = null;
		String alarm_id = ServletActionContext.getRequest().getParameter(
				"alarm_id");
		String checkid = ServletActionContext.getRequest().getParameter(
				"checkid");
		String targetName = ServletActionContext.getRequest().getParameter(
				"targetName");
		String staTime = ServletActionContext.getRequest().getParameter(
				"staTime");
		String endTime = ServletActionContext.getRequest().getParameter(
				"endTime");
		String pattern = ServletActionContext.getRequest().getParameter(
				"pattern");
		String pulse = ServletActionContext.getRequest().getParameter("pulse");
		String id = ServletActionContext.getRequest().getParameter("id");

		String sql = "";
		Connection con = db.getConnection();
		try {
			if (checkid != null && !checkid.equals("-1")) {
				sql = "update t_s_herald set alarm = ?,  alarm_target = ? ,target_name =?,staTime = ?,endTime=?,pattern=?   where id = ?";
			} else {
				sql = "update t_s_herald set alarm = ? ,staTime = ?,endTime=?,pattern=?   where id = ?";
			}

			try {
				ps = con.prepareStatement(sql);
				int index = 0;
				ps.setString(++index, alarm_id);
				if (checkid != null && !checkid.equals("-1")) {
					ps.setString(++index, checkid);
					ps.setString(++index, targetName);
				}
				ps.setString(++index, staTime);
				ps.setString(++index, endTime);
				ps.setString(++index, pattern);
				ps.setString(++index, id);
				int rs = ps.executeUpdate();
				if (rs > 0) {
			//		System.out.println("保存成功!!!");
				}
				JSONObject json = new JSONObject();
				json.put("success", true);
				HttpServletResponse response = ServletActionContext
						.getResponse();
				response.setCharacterEncoding("UTF-8");
				PrintWriter out = response.getWriter();
				out.print(json);
				out.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				ps.close();
				con.close();
			} catch (Exception ex1) {
				ex1.printStackTrace();
			}
		}
	}

	// 删除 传报
	public void deleteHeral() {
		ConnDB db = new ConnDB();
		Statement ps = null;
		HttpServletRequest req = ServletActionContext.getRequest();
		String id = req.getParameter("heraldIds");
		id = id.substring(0, id.length() - 1);
		Connection con = db.getConnection();
		try {
			con.setAutoCommit(false);
			ps = con.createStatement();

			boolean bool = false;

			String sql = "delete t_s_herald  where id in( " + id + " )";
			bool = ps.execute(sql);
			ps = con.createStatement();
			if (bool) {
			//	System.out.println("保存成功!!!");
			}
			JSONObject json = new JSONObject();
			json.put("success", true);
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			out.print(json);
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		} finally {
			try {
				con.commit();
				ps.close();
				con.close();
			} catch (Exception ex1) {
				ex1.printStackTrace();
			}
		}
	}
	
	
	
	// 修改报表 发送 类型
	public void updateFinring() {
		ConnDB db = new ConnDB();
		Statement ps = null;
		HttpServletRequest req = ServletActionContext.getRequest();
		String finring = req.getParameter("finring");
		String id = req.getParameter("sendIds");
		Connection con = db.getConnection();
		try {
			con.setAutoCommit(false);
			ps = con.createStatement();
			boolean bool = false;
			String sql = "update t_s_handle set pulse= '"+finring+"' where id in ("+id+"0)";
			bool = ps.execute(sql);
			ps = con.createStatement();
			if (bool) {
			//	System.out.println("保存成功!!!");
			}
			JSONObject json = new JSONObject();
			json.put("success", true);
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			out.print(json);
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		} finally {
			try {
				con.commit();
				ps.close();
				con.close();
			} catch (Exception ex1) {
				ex1.printStackTrace();
			}
		}
	}

	// 获取选中传报的对象
	public void getHeral() {
		ConnDB db = new ConnDB();
		ResultSet rs = null;
		List<Map<String, String>> datalist = new ArrayList<Map<String, String>>();
		JSONArray datas = new JSONArray();
		String para = ServletActionContext.getRequest()
				.getParameter("heraldId");
		Connection con = db.getConnection();
		PreparedStatement ps = null;
		try {
			String sql = "select h.id,d.name,alarm,alarm_target,target_name,statime,endtime,pattern,pulse   from dbo.t_s_herald h,dictionary d where h.alarm = d.id and h.id = "
					+ para;

			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
			Map<String, String> mapdata = new HashMap<String, String>();
			try {
				while (rs.next()) {

					mapdata.put("id", rs.getString(1));
					mapdata.put("alarm_name", rs.getString(2));
					mapdata.put("alarm_id", rs.getString(3));
					mapdata.put("alarm_target", rs.getString(4));
					mapdata.put("target_name", rs.getString(5));
					mapdata.put("heraldstaTime", rs.getString(6));
					mapdata.put("heraldendTime", rs.getString(7));
					mapdata.put("pattern", rs.getString(8));
					mapdata.put("pulse", rs.getString(9));

				}
				JSONObject json = JSONObject.fromObject(mapdata);
				datas = JSONArray.fromObject(datalist);
				json.put("success", true);
				json.put("datas", datas);
				HttpServletResponse response = ServletActionContext
						.getResponse();
				response.setCharacterEncoding("UTF-8");
				PrintWriter out = response.getWriter();
				out.print(json.toString());
				out.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				ps.close();
				rs.close();
				con.close();
			} catch (Exception ex1) {
				ex1.printStackTrace();
			}
		}
	}

	// 或取上次保存的值
	public List getHeralSelected(String heraldId) {
		ConnDB db = new ConnDB();
		ResultSet rs = null;
		List list = new ArrayList<Map<String, String>>();
		List<Map<String, String>> datalist = new ArrayList<Map<String, String>>();
		JSONArray datas = new JSONArray();

		Connection con = db.getConnection();
		PreparedStatement ps = null;
		try {
			String sql = "select h.id,d.name,alarm,alarm_target,target_name from dbo.t_s_herald h,dictionary d where h.alarm = d.id and h.id = "
					+ heraldId;

			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
			Map<String, String> mapdata = new HashMap<String, String>();
			String target = "";
			while (rs.next()) {

				target = rs.getString(4);

				list.add(target);

			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				ps.close();
				rs.close();
				con.close();
			} catch (Exception ex1) {
				ex1.printStackTrace();
			}
			return list;
		}
	}
	
	// 或取上次保存的值
	public List getHandleSelected(String heraldId) {
		ConnDB db = new ConnDB();
		ResultSet rs = null;
		List list = new ArrayList<Map<String, String>>();
		Connection con = db.getConnection();
		PreparedStatement ps = null;
		try {
			String sql = "select h.id,d.name,supervise,supervise_target,target_name from dbo.t_s_handle h,dictionary d where h.supervise = d.id and h.id = "
					+ heraldId;
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
			String target = "";
			while (rs.next()) {
				target = rs.getString(4);
				list.add(target);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				ps.close();
				rs.close();
				con.close();
			} catch (Exception ex1) {
				ex1.printStackTrace();
			}
			return list;
		}
	}

}
